﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    /// <summary>
    /// Batch 的摘要说明
    /// </summary>
    public class Batch : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            List<zTreeUtil.zTreeNode> lsZTreeNodes = new List<zTreeUtil.zTreeNode>();
            zTreeUtil.zTreeNode node = null;
            string parentId = context.Request.Params["id"];
            string responseData = "";
            if (parentId == null)
            {
                int count = new Honestysoft.Financing.BusinessLogic.Audit("").GetBatchCount() ;
                node = new zTreeUtil.zTreeNode();
                node.id = "0";
                node.isParent = count> 0;
                node.name = "所有批次(共" + count.ToString() + "批)";
                node.target = "ifrmInfo";
                node.url = "Bu_AuditBatch_List.aspx?ID=0";
                lsZTreeNodes.Add(node);
            }
            else
            {
                //得到批次
                OrderParameters order = new OrderParameters();
                order.Add("BatchNo", OrderParameters.OrderDirection.Desc);
                DataTable dtBatch = new BaseBusinessLogic<Bu_AuditBatch>().GetTable(null, order);
                if (dtBatch != null && dtBatch.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtBatch.Rows)
                    {
                        node = new zTreeUtil.zTreeNode();
                        node.id = dr["ID"].ToString();
                        node.isParent = false;
                        node.name = "（" + dr["BatchNo"] + "）第" + ChineseNum.GetChineseNum(dr["BatchNo"].ToString()) + "批";
                        node.target = "ifrmInfo";
                        if (dr["BatchStatus"].ToString() == ((int)EnumItem.BatchStatus.Active).ToString())
                        {
                            node.icon = "../../images/set.gif";
                        }
                        else if (dr["BatchStatus"].ToString() == ((int)EnumItem.BatchStatus.Lock).ToString())
                        {
                            node.icon = "../../images/luck.gif";
                        }
                        else if (dr["BatchStatus"].ToString() == ((int)EnumItem.BatchStatus.Output).ToString())
                        {
                            node.icon = "../../images/pic6.gif";
                        }
                        node.url = "BatchInfo.aspx?ID=" + node.id;
                        lsZTreeNodes.Add(node);
                    }
                }
            }
            responseData = zTreeUtil.GetJsonString(lsZTreeNodes);
            context.Response.Write(responseData);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}